﻿using System;
using System.Configuration;
using System.IO;
using System.Web;
using DFT.DB;
using DFT.Infos;
using DFT.Mail;

namespace DFT
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            failLoginMsg.Text = " ";
            failRegMsg.Text = " ";
            if (!Page.IsPostBack)
            {
                if (Session["userInfo"] != null)
                {
                    if (Request.QueryString["returnUrl"] != null)
                    {
                        Response.Redirect(Request.QueryString["returnUrl"]);
                    }
                    else
                    {
                        Response.Redirect(ConfigurationManager.AppSettings["MainPage"]);
                    }
                }
                else
                {
                    if (Request.Cookies["userEmail"] != null && Request.Cookies["userPass"] != null)
                    {
                        UserInfo userInfo = new UserInfo();
                        String returnCdoe = "";
                        if (DBManager.ValidateUser(Request.Cookies["userEmail"].Value, Request.Cookies["userPass"].Value, ref returnCdoe, ref userInfo))
                        {
                            Session["userInfo"] = userInfo;
                            if (Request.QueryString["returnUrl"] != null)
                            {
                                Response.Redirect(Request.QueryString["returnUrl"]);
                            }
                            else
                            {
                                Response.Redirect(ConfigurationManager.AppSettings["MainPage"]);
                            }
                        }
                        else
                        {
                            Request.Cookies["userEmail"].Expires = DateTime.Now.AddDays(-1);
                            Request.Cookies["userPass"].Expires = DateTime.Now.AddDays(-1);
                        }
                    }
                }
            }
        }

        protected void lLogin_Click(Object sender, EventArgs e)
        {
            String userEmail = Request.Form["loginEmail"];
            String userPass = Request.Form["loginPass"];

            UserInfo userInfo = new UserInfo();
            String returnCdoe = "";
            if (DBManager.ValidateUser(userEmail, userPass, ref returnCdoe, ref userInfo))
            {
                if (lRem.Checked)
                {
                    HttpCookie userEmailCookie = new HttpCookie("userEmail", userEmail);
                    userEmailCookie.Expires = DateTime.Today.AddDays(10);
                    Response.Cookies.Add(userEmailCookie);

                    HttpCookie userPassCookie = new HttpCookie("userPass", userPass);
                    userPassCookie.Expires = DateTime.Today.AddDays(10);
                    Response.Cookies.Add(userPassCookie);
                }
                Session["userInfo"] = userInfo;
                if (Request.QueryString["returnUrl"] != null)
                {
                    Response.Redirect(Request.QueryString["returnUrl"]);
                }
                else
                {
                    Response.Redirect(ConfigurationManager.AppSettings["MainPage"]);
                }
            }
            else
            {
                switch (returnCdoe)
                {
                    case "1":
                        failLoginMsg.Text = "هذا البريد غير موجود في سجلاتنا يرجى التحقق";
                        break;
                    case "2":
                        failLoginMsg.Text = "يوجد خطأ في كلمة المرور";
                        break;
                    default:
                        failLoginMsg.Text = returnCdoe;
                        break;
                }
            }

        }

        protected void rReg_Click(Object sender, EventArgs e)
        {
            String userFullName = Request.Form["rName"];
            String userEmail = Request.Form["rEmail"];
            String userPass = Request.Form["rPass"];

            UserInfo userInfo = new UserInfo();
            String returnCdoe = "";

            if (DBManager.CreateNewUser(userFullName, userEmail, userPass, ref returnCdoe))
            {
                if (Boolean.Parse(ConfigurationManager.AppSettings["MailEnabled"]))
                {
                    String templatePath = HttpContext.Current.Server.MapPath("~//EmailTemplates//CompleteRegistration.htm");
                    String body = File.ReadAllText(templatePath);
                    body = body.Replace("@Name", userFullName);
                    MailHelper.SendMailMessage(userEmail, "sub", body);
                }
                if (DBManager.ValidateUser(userEmail, userPass, ref returnCdoe, ref userInfo))
                {
                    Session["userInfo"] = userInfo;
                    Response.Redirect(ConfigurationManager.AppSettings["MainPage"]);
                }
            }
            else
            {
                switch (returnCdoe)
                {
                    case "1":
                        failRegMsg.Text = "هذا البريد موجود مسبقا يرجى التحقق";
                        break;
                    default:
                        failRegMsg.Text = returnCdoe;
                        break;
                }
            }
        }
    }
}